package com.example.coursehub.course.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程实体类
 * 改进了原有设计，添加了审计字段和状态管理
 */
@Getter
@Setter
@Entity
@Table(name = "course", schema = "courseHub")
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class Course {
    
    @Id
    @Column(name = "course_id", nullable = false, length = 100)
    private String courseId;

    @NotBlank(message = "课程名称不能为空")
    @Size(max = 100, message = "课程名称长度不能超过100个字符")
    @Column(name = "course_name", nullable = false, length = 100)
    private String courseName;

    @Size(max = 1000, message = "课程描述长度不能超过1000个字符")
    @Column(name = "course", length = 1000)
    private String courseDescription;

    @Size(max = 200, message = "教学目标长度不能超过200个字符")
    @Column(name = "teaching_goal", length = 200)
    private String teachingGoal;

    @NotBlank(message = "考核方式不能为空")
    @Size(max = 100, message = "考核方式长度不能超过100个字符")
    @Column(name = "examination_method", nullable = false, length = 100)
    private String examinationMethod;

    /**
     * 课程状态：0-草稿，1-发布，2-归档
     */
    @Column(name = "status", nullable = false)
    private Integer status = 0;

    /**
     * 课程创建者ID（教师ID）
     */
    @Column(name = "creator_id", length = 100)
    private String creatorId;

    /**
     * 课程学分
     */
    @Column(name = "credits")
    private Integer credits;

    /**
     * 课程学时
     */
    @Column(name = "hours")
    private Integer hours;

    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    /**
     * 课程章节（一对多关系）
     */
    @OneToMany(mappedBy = "course", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Chapter> chapters;

    /**
     * 课程资料（一对多关系）
     */
    @OneToMany(mappedBy = "course", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Material> materials;

    /**
     * 课程状态枚举
     */
    public enum CourseStatus {
        DRAFT(0, "草稿"),
        PUBLISHED(1, "已发布"),
        ARCHIVED(2, "已归档");

        private final Integer code;
        private final String description;

        CourseStatus(Integer code, String description) {
            this.code = code;
            this.description = description;
        }

        public Integer getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }
    }
}